/**
 * @author Daniel
 */

/**
 * required ejs
 * required css
 * .normal-button {
 * 	
 * }
 * .normal-button.disabled {
 * 	
 * }
 * .normal-button.selected {
 * 	
 * }
 * .normal-button .txt {
 * 	
 * }
 */
(function($) {
	
	var numPager = {
		
		options: {
			totalItemCount: 0
			, displayItemCount: 10
			, displayNumCount: 4 //显示的数字按钮最大个数
			, btnCls: {
				firstPageBtnCls: 'normal-button'
				, lastPageBtnCls: 'normal-button'
				, prePageBtnCls: 'normal-button'
				, nextPageBtnCls: 'normal-button'
				, numBtnCls: 'normal-button'
			}
			// , onPageChange: function(e, data) {}  // data: {currentPageNum:2}
		}
		
		, _events: {
			pageChange: 'onPageChange'
		}
		
		, _ejs: require('ejs')
		
		, _currentPageNum: 1
		
		, _totalPageNum: 0
		
		, _totalItemCount: 0
		
		, _displayItemCount: 0
		
		, _curNumRange: [0, 0] //当前显示数字按钮的数字范围
		
		, _numBtnHtml: [
			, '	<a num="<%=num%>" class="<%=numBtnCls%>" >'
			, '		<span class="txt blod"><%=num%></span>'
			, '	</a>'
		].join(' ')
		
		/* jquery DOM object */
		, _firstPageBtn: null
		, _lastPageBtn: null
		, _prePageBtn: null
		, _nextPageBtn: null
		, _numBtnContainer: null		
		
		, _create: function() {
			var self = this
				, container = self.element			
				, pageHtml = [
					'	<a id="firstPageBtn" class="<%=firstPageBtnCls%>" >'
					, '		<span class="txt blod">首页</span>'
					, '	</a>'
					, '	<a id="prePageBtn" class="<%=prePageBtnCls%>" >'
					, '		<span class="txt blod">上一页</span>'
					, '	</a>'
					, ' <span id="numBtnContainer"></span>'
					, '	<a id="nextPageBtn" class="<%=nextPageBtnCls%>" >'
					, '		<span class="txt blod">下一页</span>'
					, '	</a>'
					, '	<a id="lastPageBtn" class="<%=lastPageBtnCls%>" >'
					, '		<span class="txt blod">最后</span>'
					, '	</a>'
				].join('')
			;
			
			container.html(self._ejs.render(pageHtml, self.options.btnCls));
			
			self._initDOMObjects();
			
			self._displayItemCount = self.options.displayItemCount;
			
			self.setTotalItemCount(self.options.totalItemCount);
			
			self._bindEvents();
			
			self._changeBtnStatus();
		}
		
		, _setOption: function(key, value) {
			$.Widget.prototype._setOption.apply(this, arguments);
		}
		
		, _initDOMObjects: function() {
			var self = this
				, container = self.element
				;
				
			self._firstPageBtn = container.find('#firstPageBtn');
			self._lastPageBtn = container.find('#lastPageBtn');
			self._prePageBtn = container.find('#prePageBtn');
			self._nextPageBtn = container.find('#nextPageBtn');
			self._numBtnContainer = container.find('#numBtnContainer');
		}
		
		, _bindEvents: function() {
			var self = this
				, firstPageBtn = self._firstPageBtn
				, lastPageBtn = self._lastPageBtn
				, prePageBtn = self._prePageBtn
				, nextPageBtn = self._nextPageBtn
				;
			
			firstPageBtn.bind('click', function() {
				if (firstPageBtn.hasClass('disabled')) return false;
				self._currentPageNum = 1;
				self._changeBtnStatus();
				self._displayNumBtns();
				self._setNumItemSelectedStyle();
				self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
			});
			lastPageBtn.bind('click', function() {
				if (lastPageBtn.hasClass('disabled')) return false;
				self._currentPageNum = self._totalPageNum;
				self._changeBtnStatus();
				self._displayNumBtns();
				self._setNumItemSelectedStyle();
				self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
			});
			prePageBtn.bind('click', function() {
				if (prePageBtn.hasClass('disabled')) return false;
				self._currentPageNum--;
				if (self._currentPageNum <= 0) {
					self._currentPageNum = 1;
				}
				self._changeBtnStatus();
				self._displayNumBtns();
				self._setNumItemSelectedStyle();
				self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
			});
			nextPageBtn.bind('click', function() {
				if (nextPageBtn.hasClass('disabled')) return false;
				self._currentPageNum++;
				if (self._currentPageNum > self._totalPageNum) {
					self._currentPageNum = self._totalPageNum;
				}
				self._changeBtnStatus();
				self._displayNumBtns();
				self._setNumItemSelectedStyle();
				self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
			});
		}
		
		, _changeBtnStatus: function() {
			var self = this
				, firstPageBtn = self._firstPageBtn
				, lastPageBtn = self._lastPageBtn
				, prePageBtn = self._prePageBtn
				, nextPageBtn = self._nextPageBtn
				, currentPageNum = self._currentPageNum
				, totalPageNum = self._totalPageNum
				;
				
			firstPageBtn.removeClass('disabled');
			lastPageBtn.removeClass('disabled');
			prePageBtn.removeClass('disabled');
			nextPageBtn.removeClass('disabled');
			
			if (totalPageNum == 0  || totalPageNum == 1) {
				firstPageBtn.addClass('disabled');
				prePageBtn.addClass('disabled');	
				nextPageBtn.addClass('disabled');
				lastPageBtn.addClass('disabled');			
			} else {
				switch (currentPageNum) {
					case 1: 
						firstPageBtn.addClass('disabled');
						prePageBtn.addClass('disabled');
						break;
					case totalPageNum:
						nextPageBtn.addClass('disabled');
						lastPageBtn.addClass('disabled');
						break;
					default:
						break;
				}	
			}
			
		}
		
		, _displayNumBtns: function() {
			var self = this
				, currentPageNum = self._currentPageNum
				, totalPageNum = self._totalPageNum
				, displayNumCount = self.options.displayNumCount
				, numBtnContainer = self._numBtnContainer
				, numBtnHtml = self._numBtnHtml
				, curNumRange = self._curNumRange
				;	
				
			if (totalPageNum == 0) {
				numBtnContainer.html('');
				curNumRange = [0, 0];
				return false;
			}
			
			var numPageNo = currentPageNum / displayNumCount;
			numPageNo = numPageNo > parseInt(numPageNo) ? parseInt(numPageNo) + 1 : numPageNo;
			var firstNum = (numPageNo - 1) * displayNumCount + 1;
			var lastNum = numPageNo	* displayNumCount;	
			
			if (totalPageNum>=lastNum && (firstNum >= curNumRange[0] && lastNum <= curNumRange[1])) { //处于范围内，不作任何处理
				return false;
			}
			
			numBtnContainer.html('');
			
			var maxNum = totalPageNum >= lastNum ? lastNum : totalPageNum;
			for (var i=firstNum; i<=maxNum; i++) {
				numBtnContainer.append(self._ejs.render(numBtnHtml, {num:i, numBtnCls:self.options.btnCls.numBtnCls}));
			}
			
			curNumRange = [firstNum, maxNum];
			
			// bind events
			var numBtns = numBtnContainer.find('a');
			numBtns.each(function(idx, item) {
				item = $(item);
				item.bind('click', function() {
					numBtns.removeClass('selected');
					item.addClass('selected');
					self._currentPageNum = item.text().trim();
					self._changeBtnStatus();
					self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});
				});
			});
		}
		
		, _setNumItemSelectedStyle: function() {
			var self = this
				, currentPageNum = self._currentPageNum	
				, numBtnContainer = self._numBtnContainer
				, numBtns = numBtnContainer.find('a')
				, selectedBtn = numBtnContainer.find('a[num="'+currentPageNum+'"]')
				;
			
			var numBtns = numBtnContainer.find('a');
			numBtns.removeClass('selected');
			selectedBtn.addClass('selected');
		}
		
		, setTotalItemCount: function(totalCount) {
			var self = this
				;	
			
			self._totalItemCount = totalCount;	
			self._totalPageNum = self._totalItemCount % self._displayItemCount == 0 ? self._totalItemCount / self._displayItemCount : parseInt(self._totalItemCount / self._displayItemCount) + 1;
			
			self._changeBtnStatus();
			self._displayNumBtns();
			self._setNumItemSelectedStyle();
		}
		
		, getCurrentPageNum: function() {
			return this._currentPageNum;
		}
		
		, setCurrentPageNum: function(pageNum) {
			var self = this
				, totalPageNum = self._totalPageNum
				, currentPageNum = self._currentPageNum
				;
				
			if (pageNum <= 0 || totalPageNum == 0) {
				pageNum = 1;
			} else if (pageNum > totalPageNum) {
				pageNum = self._totalPageNum;
			}
			
			self._currentPageNum = pageNum;		
			self._changeBtnStatus();
			self._displayNumBtns();
			self._setNumItemSelectedStyle();
			self._trigger(self._events.pageChange, new $.Event(self._events.pageChange), {currentPageNum:self._currentPageNum});	
			return true;		
		}
		
		, destroy: function() {
			$.Widget.prototype.destroy.call(this);
		}
		
	};
	
	$.widget('ft.numPager', numPager);
})(jQuery);